package oop.controller.action.user;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oop.controller.action.DefaultAction;
import oop.db.Database;

@SuppressWarnings("unused")
public class ForgetPassAction extends UserManagerAction {

	@Override
	public void perform()
			throws Exception {
		if ("Send Password".equals(request.getParameter("sendPassword"))) {
			if (isValidEmail(request, response) == false) {
				// Email sai qui cach
				request.setAttribute("invalidEmail", "true");
			} else {

				Connection conn;
				try {
					conn = Database.get().getConnection();
					Statement stmt = conn.createStatement();
					String sql = "SELECT user_name, user_pass, user_email "
							+ "FROM tblUser " + "WHERE user_email = '"
							+ request.getParameter("userEmail") + "'";
					ResultSet rs = stmt.executeQuery(sql);
					if (rs.next()) {
						// tim thay nguoi dung
						// mail to ng dung //-------HOW?-----------//

						request.setAttribute("passSent", "true");
					} else {
						// Khong ton tai nguoi dung
						request.setAttribute("userNotExist", "true");
					}

				} catch (SQLException e) {
					request.setAttribute("sqlError", "true");
					e.printStackTrace();
				}
			}
		}

	}

}
